package com.android.hedgehog.puzzles;

import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.Assets;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input.TouchEvent;

public class CardMatchScreen extends PuzzleScreen {
	enum GameState {
		Start, Card0, Card1, Incorrect, Finish
	}
	CardMatchField deck;
	int difficulty;
	Graphics graphics;
	GameState state;
	double totalScreenTime;
	double resetTime;
	String resultMsg;

	public CardMatchScreen(Game game, int gameType, int difficulty,
			int puzzleNum) {
		this.difficulty = difficulty;
		this.gameType = gameType;
		instanceNumber = puzzleNum;
		if (instanceNumber < 2)
			state = GameState.Start;
		else
			state = GameState.Card0;
		graphics = game.getGraphics();
		deck = new CardMatchField(difficulty);
	}

	@Override
	public void update(List<TouchEvent> touchEvents, double deltaTime) {
		totalScreenTime += deltaTime;
		if (state == GameState.Start) {
			if (totalScreenTime > 2.0)
				state = GameState.Card0;
		}

		if (state == GameState.Incorrect) {
			resetTime += deltaTime;

			if (resetTime > 1.5) {
				resetTime = 0.0;
				deck.reset();
				state = GameState.Card0;
			}
		}

		if (state == GameState.Finish) {
			resetTime += deltaTime;
			if (resetTime > 1.5) {
				instanceOver = true;
			}
		}

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if (event.y > 90 && event.y < 165) {// ROW 1
					if (event.x > 40 && event.x < 90) {
						flipCard(0);
					}
					if (event.x > 100 && event.x < 150) {
						flipCard(1);
						return;
					}
					if (event.x > 160 && event.x < 210) {
						flipCard(2);
					}
					if (event.x > 220 && event.x < 270) {
						flipCard(3);
					}
				}// ROW 1
				if (event.y > 175 && event.y < 250) {// ROW 2
					if (event.x > 40 && event.x < 90) {
						flipCard(4);
					}
					if (event.x > 100 && event.x < 150) {
						flipCard(5);
						return;
					}
					if (event.x > 160 && event.x < 210) {
						flipCard(6);
					}
					if (event.x > 220 && event.x < 270) {
						flipCard(7);
					}

				}// ROW 2
				if (event.y > 260 && event.y < 335) {// ROW 3
					if (event.x > 40 && event.x < 90) {
						flipCard(8);
					}
					if (event.x > 100 && event.x < 150) {
						flipCard(9);
						return;
					}
					if (event.x > 160 && event.x < 210) {
						flipCard(10);
					}
					if (event.x > 220 && event.x < 270) {
						flipCard(11);
					}
				}// ROW 3
				if (event.y > 345 && event.y < 420) {// ROW 4
					if (event.x > 40 && event.x < 90) {
						flipCard(12);
					}
					if (event.x > 100 && event.x < 150) {
						flipCard(13);
						return;
					}
					if (event.x > 160 && event.x < 210) {
						flipCard(14);
					}
					if (event.x > 220 && event.x < 270) {
						flipCard(15);
					}

				}// ROW 4
			}
		}
	}
	private void flipCard(int cardNum) {
		if (state == GameState.Card0) {// Card 0
			if (deck.card[cardNum].matched == false
					&& deck.card[cardNum].flipped == false) {
				deck.card[cardNum].flipped = true;
				state = GameState.Card1;
			}
		} // Card 0

		if (state == GameState.Card1) {// Card 1
			if (deck.card[cardNum].matched == false
					&& deck.card[cardNum].flipped == false) {
				deck.card[cardNum].flipped = true;
				deck.totalTurns--;
				deck.isMatch();
				if (deck.isMatch()) {
					if (isGameOver()) {
						state = GameState.Finish;
					} else {
						deck.reset();
						state = GameState.Card0;
					}
				} else {
					if (isGameOver()) {
						state = GameState.Finish;
					} else {
						state = GameState.Incorrect;
					}
				}
			}
		} // Card 1

	}

	public boolean isGameOver() {

		if (deck.isComplete()) { // entire deck matched
			instanceScore = 5 + difficulty;
			resultMsg = "Success";
			return true;
		}
		if (deck.totalTurns < 1) {// out of turns
			resultMsg = "Fail";
			return true;
		}
		return false;

	}

	@Override
	public void present() {
		graphics.drawPixmap(Assets.blackbackground, 0, 31);

		if (state == GameState.Start) { // START
			graphics.drawSpecialText("Match Pairs of Identical Cards", 160,
					240, 16, 2, Color.RED);
		}// START

		if (state != GameState.Start) {

			for (int i = 0; i < deck.totalRows; i++) {
				for (int j = 0; j < 4; j++) {
					if (deck.card[j + (i * 4)].flipped
							|| deck.card[j + (i * 4)].matched) {
						graphics.drawPixmap(Assets.cards, 40 + (j * 60),
								90 + (i * 85),
								(deck.card[j + (i * 4)].value * 50), 0, 50, 75);
					} else {
						graphics.drawRect(40 + (j * 60), 90 + (i * 85), 50, 75,
								Color.GRAY);
						graphics.drawRect2(40 + (j * 60), 90 + (i * 85), 50,
								75, Color.DKGRAY);
					}
				}
			}
			if (state == GameState.Card0 || state == GameState.Card1
					|| state == GameState.Incorrect)
				graphics.drawText("Turns Left: " + deck.totalTurns, 160, 60,
						16, Color.WHITE);
			else {
				if (instanceScore > 0) {
					graphics.drawText(resultMsg, 160, 55, 20, Color.GREEN);
					if (gameType == 1)
						graphics.drawSpecialText("+" + instanceScore, 160, 75,
								16, 2, Color.GREEN);

				} else
					graphics.drawText(resultMsg, 160, 55, 20, Color.RED);
			}

		} // NOT START

	}
}
